package com.wnn.demo.model.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Job;
import com.wnn.demo.model.hospital.service.JobService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 职位管理
 * @author asus
 */
@RestController
@RequestMapping("/manage/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @GetMapping()
    public Result selectJobAll(@RequestParam(defaultValue = "") String selectJobId,String type,String pId){
        List<Map> pageInfo  = jobService.selectJobAll(selectJobId,type,pId);
        return new Result(ResultCode.OK,pageInfo );
    }

    /**
     * 使用treeTable的layui插件直接全部返回，他会在js层面自动形成菜单树
     * @return
     */
    @GetMapping("/findJobAll")
    public Result findJobAll(){
        List<Job> list = jobService.findJobAll();
        return new Result(ResultCode.OK ,list);
    }

    /**
     * 使用jdk 8 stream留处理的菜单树，未使用
     * @return
     */
    @GetMapping("/findJobTree")
    public Result findDeptTree(){
        List<Job> list = jobService.findJobTree();
        return new Result(ResultCode.OK ,list);
    }


    /**
     * 新增职位
     * @param job
     * @return
     */
    @PostMapping()
    public Result insertJob(@RequestBody Job job){
        int i = jobService.insertJob(job);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更新职位
     * @param job
     * @return
     */
    @PutMapping("/{id}")
    public Result updateJob(@PathVariable(value = "id")Integer id,@RequestBody Job job){
        job.setJobId(id);
        int i = jobService.updateJob(job);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }


    /**
     * 根据职位Id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(value = "id") String id) {
        int i = jobService.deleteJobById(id);
        if (i>0){
            return Result.ok();
        }
        return Result.error();
    }


    @PostMapping("/batchJob")
    public Result batchJobByIds(String ids ) {

        int i = jobService.batchJobByIds(ids);
        if (i>0){
            return Result.ok();
        }
        return Result.error();

    }

}
